local ui = class.select_panel.create('玩家-选择抽卡')
ui:set_level(99)
ui:set_select_button(3,class.card_data,300,429)
ui:hide()
ac.crad_ui = ui

for a,button in ipairs(ui.childs) do
    local x1,y1 = button:get_real_position()
    local y2 = y1 + (button.index%2==1 and 1 or -1)*200
    button.y1 = y1
    button.y2 = y2
end

local old_on_button_mouse_enter = ui.on_button_mouse_enter
function ui:on_button_mouse_enter(button)
    old_on_button_mouse_enter(self,button)
    local skill = button.skill
    if skill then
        local tip = skill:get_tip()
        self:tooltip(skill.title or skill.name,tip)
    end
end

function ui:update()
    local player = ac.player.self
    local hero = player.hero
    --动画
    local time = (self.wait_select - ac.clock()) / 1000
    for a,button in ipairs(self.childs) do
        if time>0 then
            button:set_real_position(button.x,button.y2)
            button:move_animation({button.x,button.y1},time)
        end
    end
end

--重置按钮
local refresh = ui:add_button([[ui\刷新.tga]],(1920-300)/2,660,152,46,1)
refresh.highlight = refresh:add_panel([[ui\按钮-选中遮罩.tga]]) 
refresh.highlight:hide()
ac.card_refresh = refresh

function refresh:on_button_mouse_enter()
    local player = ac.player.self
    local hero = player.hero
    local text = ('免费X%0.f'):format(hero:get('刷新次数'))
    if hero and hero:get('刷新次数')==0 then
        text = ('木材X%s'):format(player:get_data('天赋刷新价格',0) )
    end
    self:tooltip(text,'','bottom',150)
end

function refresh:on_button_clicked()
    ac.sync('玩家-刷新抽卡')
end

--关闭
local esc = ui:add_button([[ui\稍后再选.tga]],(1920-300)/2 + 180,660,152,46,1)
esc.highlight = esc:add_panel([[ui\按钮-选中遮罩.tga]]) 
esc.highlight:hide()

function esc:on_button_clicked()
    ui:hide()
end

function ui:on_button_clicked(button)
    if button.index and button.button_type=='选择按钮' and self.wait_select<ac.clock() then
        local time = 0.15
        self.wait_select = ac.clock() + time * 1000
        ac.sync(self.sync_name or self.name,button.index,self.wait_select)

        --动画
        for a,button in ipairs(self.childs) do
            if button:is_show() then
                button:set_real_position(button.x,button.y1)
                button:move_animation({button.x,button.y2},time)
            end
        end
    end 
end

ac.sync:event '玩家-选择抽卡'(function(_,player,index,time)
    local time = time - ac.clock()
    player._ck_timer = ac.wait(time,function()
        player._ck_timer = nil
        if player:is_self() and ui.wait_select <= ac.clock() then
            ui:hide()
        end
    end)
end)

ac.sync:event '玩家-刷新抽卡'(function(_,player)
    player:notify('玩家-准备抽卡',player,true)
end)

ac.sync:event '玩家-选择抽卡'(function(_,player,index)
    --local data = player:get_data('展示天赋表') or {}
    --local skill = button.skill
    --local x,y = button.icon:get_real_position()
    --local start = {
    --    x = x,
    --    y = y,
    --    w = button.iocn.w,
    --    h = button.iocn.h,
    --}
    --ac.game:notify('动画-抽卡显示',skill.art,start,#data)
    player:notify('玩家-选择抽卡',player,index)
    
end)

ac.game:event '卡池-显示抽卡'(function(_,player,data)
    if player._ck_timer then
        player._ck_timer:remove()
        player._ck_timer = nil
    end
    if player:is_self() then
        ui:set_data(data,0.15)
    end
end)
